package com.daipresents.gfmon.action;

import java.util.Properties;

import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.monitor.MonitorNotification;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

public class EmailAction implements NotificationListener, EmailActionMBean {

    private Logger logger = Logger.getLogger(EmailAction.class);
    private String host;
    private String user;
    private String sender;
    private String receiver;

    public EmailAction() {
        this.host = "";
        this.user = "";
        this.sender = "";
        this.receiver = "";
    }

    public void handleNotification(Notification notification, Object handback) {
            String observedAttribute = ((MonitorNotification) notification).getObservedAttribute();
            String notificationMessage;
            if (notification.getType().equals(MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED)) {
                    notificationMessage = observedAttribute + " exceeded high threshold.";
            } else {
                    notificationMessage = observedAttribute + " exceeded low threshold.";
            }
            logger.debug("NOTIFICATION:" + notificationMessage);

            if (host == null || user == null || sender == null || receiver == null) {
                    logger.warn("Cannot send email as AlertEmail MBean properties not set.");
                    return;
            }
            
            Properties props = System.getProperties();
            props.put("mail.host", host);
            props.put("mail.user", user);
            props.put("mail.from", sender);
            Session session = Session.getDefaultInstance(props);

            try {
                    MimeMessage msg = new MimeMessage(session);
                    msg.setSubject("GF Self Management Alert");
                    msg.setRecipients(Message.RecipientType.TO, receiver);
                    msg.setText(notificationMessage);
                    Transport.send(msg);
                    logger.debug("Self Management alert email sent...");
            } catch (MessagingException mex) {
                    logger.error(null, mex);
            }
    }

    
    @Override
    public String getHost() {
        return this.host;
    }

    @Override
    public String getReceiver() {
        return this.receiver;
    }

    @Override
    public String getSender() {
        return this.sender;
    }

    @Override
    public String getUser() {
        return this.user;
    }

    @Override
    public void setHost(String host) {
        this.host = host;
    }

    @Override
    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    @Override
    public void setSender(String sender) {
        this.sender = sender;
    }

    @Override
    public void setUser(String user) {
        this.user = user;
    }
}
